<%@Page Language="VB" %>
<%@Import Namespace="System.Xml" %>
<%@Import Namespace="System.Xml.Xpath" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Searching an XML document with an XPathNavigator</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Searching an XML document with an XPathNavigator</span><hr />
<!--------------------------------------------------------------------------->

<div id="outDocURL" runat="server"></div>
<div id="outError" runat="server">&nbsp;</div>
<div id="outResults" runat="server"></div>

<script language="vb" runat="server">

Sub Page_Load()

   'create physical path to booklist.xml sample file (in same folder as ASPX page)
   Dim strCurrentPath As String = Request.PhysicalPath
   Dim strXMLPath As String = Left(strCurrentPath, InStrRev(strCurrentPath, "\")) & "booklist.xml"

   'declare a variable within current scope to hold an XPathDocument
   Dim objXPathDoc As XPathDocument

   Try

      'create XPathDocument object and load the XML file
      objXPathDoc = New XPathDocument(strXMLPath)
      outDocURL.innerHTML = "Loaded file: <b>" & strXMLPath & "</b>"

   Catch objError As Exception

      'display error details
      outError.innerHTML = "<b>* Error while accessing document</b>.<br />" _
          & objError.Message & "<br />" & objError.Source
      Exit Sub  ' and stop execution

   End Try

   'now ready to parse the XML document
   'it must be well-formed to have loaded without error

   'create a new XPathNavigator object using the XPathDocument object
   Dim objXPNav As XPathNavigator = objXPathDoc.CreateNavigator()

   'create a string to hold the matching values found
   Dim strResults As String = "<b>List of authors</b>:<br />"

   'select all the AuthorName nodes into an XPathNodeIterator object
   'using an XPath expression
   Dim objXPIter As XPathNodeIterator
   objXPIter = objXPNav.Select("descendant::AuthorName")

   'iterate through the nodes. Each "node" in the XPathNodeIterator is
   'itself an XPathNavigator, so Name and Value properties are available
   Do While objXPIter.MoveNext()

         'get the value and add to the 'results' string
         strResults += objXPIter.Current.Value & "<br />"

   Loop

   outResults.innerHTML = strResults   'display the result

End Sub

</script>

<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
